package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.dto.DailyTrainCarriageGroupDTO;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.DailyTrainCarriageMapper;
import com.ruoyi.business.domain.DailyTrainCarriage;
import com.ruoyi.business.service.IDailyTrainCarriageService;

/**
 * 每日车厢Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class DailyTrainCarriageServiceImpl implements IDailyTrainCarriageService 
{
    @Autowired
    private DailyTrainCarriageMapper dailyTrainCarriageMapper;

    /**
     * 查询每日车厢
     * 
     * @param id 每日车厢主键
     * @return 每日车厢
     */
    @Override
    public DailyTrainCarriage selectDailyTrainCarriageById(Long id)
    {
        return dailyTrainCarriageMapper.selectDailyTrainCarriageById(id);
    }

    /**
     * 查询每日车厢列表
     * 
     * @param dailyTrainCarriage 每日车厢
     * @return 每日车厢
     */
    @Override
    public List<DailyTrainCarriage> selectDailyTrainCarriageList(DailyTrainCarriage dailyTrainCarriage)
    {
        return dailyTrainCarriageMapper.selectDailyTrainCarriageList(dailyTrainCarriage);
    }

    /**
     * 新增每日车厢
     * 
     * @param dailyTrainCarriage 每日车厢
     * @return 结果
     */
    @Override
    public int insertDailyTrainCarriage(DailyTrainCarriage dailyTrainCarriage)
    {
        dailyTrainCarriage.setCreateTime(DateUtils.getNowDate());
        return dailyTrainCarriageMapper.insertDailyTrainCarriage(dailyTrainCarriage);
    }

    /**
     * 修改每日车厢
     * 
     * @param dailyTrainCarriage 每日车厢
     * @return 结果
     */
    @Override
    public int updateDailyTrainCarriage(DailyTrainCarriage dailyTrainCarriage)
    {
        dailyTrainCarriage.setUpdateTime(DateUtils.getNowDate());
        return dailyTrainCarriageMapper.updateDailyTrainCarriage(dailyTrainCarriage);
    }

    /**
     * 批量删除每日车厢
     * 
     * @param ids 需要删除的每日车厢主键
     * @return 结果
     */
    @Override
    public int deleteDailyTrainCarriageByIds(Long[] ids)
    {
        return dailyTrainCarriageMapper.deleteDailyTrainCarriageByIds(ids);
    }

    /**
     * 删除每日车厢信息
     * 
     * @param id 每日车厢主键
     * @return 结果
     */
    @Override
    public int deleteDailyTrainCarriageById(Long id)
    {
        return dailyTrainCarriageMapper.deleteDailyTrainCarriageById(id);
    }

    @Override
    public List<DailyTrainCarriageGroupDTO> selectDailyTrainCarriageGroupByTrainCode(DailyTrainCarriage dailyTrainCarriage) {
        // 获取所有符合条件的车厢数据(不分页)
        List<DailyTrainCarriage> allCarriages = dailyTrainCarriageMapper.selectDailyTrainCarriageList(dailyTrainCarriage);

        // 按trainCode分组
        Map<String, List<DailyTrainCarriage>> carriageGroups = allCarriages.stream()
                .collect(Collectors.groupingBy(DailyTrainCarriage::getTrainCode));

        // 转换为DTO对象
        List<DailyTrainCarriageGroupDTO> result = new ArrayList<>();

        for (Map.Entry<String, List<DailyTrainCarriage>> entry : carriageGroups.entrySet()) {
            String trainCode = entry.getKey();
            List<DailyTrainCarriage> carriages = entry.getValue();

            DailyTrainCarriageGroupDTO groupDTO = new DailyTrainCarriageGroupDTO();
            groupDTO.setTrainCode(trainCode);
            groupDTO.setCarriages(carriages);

            result.add(groupDTO);
        }

        return result;
    }
    
    @Override
    public List<String> selectDistinctTrainCodes(DailyTrainCarriage dailyTrainCarriage, int pageSize, int offset) {
        return dailyTrainCarriageMapper.selectDistinctTrainCodes(dailyTrainCarriage, pageSize, offset);
    }
    
    @Override
    public int countDistinctTrainCodes(DailyTrainCarriage dailyTrainCarriage) {
        return dailyTrainCarriageMapper.countDistinctTrainCodes(dailyTrainCarriage);
    }
    
    @Override
    public List<DailyTrainCarriage> selectDailyTrainCarriageByTrainCodes(DailyTrainCarriage dailyTrainCarriage) {
        return dailyTrainCarriageMapper.selectDailyTrainCarriageByTrainCodes(dailyTrainCarriage);
    }

}
